package com.study.bindview_compiler;

import com.study.bindviewannotation.OnClick;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;

/**
 * 作者： zjf 10/15/20 5:19 PM
 * 描述：
 */
public class OnClickMethod {
    private final Name methodName;
    private final int[] ids;

    public OnClickMethod(Element element) {
        if (element.getKind() != ElementKind.METHOD) {
            throw new RuntimeException(OnClick.class.getName() + ":必须作用于方法上");
        }
        ExecutableElement executableElement = (ExecutableElement) element;

        methodName = element.getSimpleName();
        //获取到注解
        OnClick onClick = element.getAnnotation(OnClick.class);
        ids = onClick.value();
        for (int id : ids) {
            if (id < 0) {
                throw new RuntimeException("资源ID不能小于0");
            }
        }
        List<? extends VariableElement> params = executableElement.getParameters();
        if (params.size() != 1) {
            throw new RuntimeException("被" + OnClick.class.getName() + "注解的方法,必须有且只有一个View参数");
        }
    }

    public Name getMethodName() {
        return methodName;
    }

    public int[] getIds() {
        return ids;
    }
}
